from fastapi import APIRouter, Query,UploadFile
from pandas.plotting import table
from tortoise.expressions import Q
from tortoise.functions import Sum
from app.controllers.saas.cases import cases_controller
from app.controllers.system.user import user_controller
from app.core.ctx import CTX_USER_ID
from app.core.dependency import DependCustomerAuth
from app.models import ConsumablesUse
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.saas import *
from app.settings.config import settings
from datetime import datetime, timedelta
import calendar

saas_cases_router = APIRouter()
model_name = "病例"


@saas_cases_router.get("/list", summary=f"查看用户{model_name}列表", dependencies=[DependCustomerAuth])
async def do_settings_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        customer_id: int = Query(0, description="用户id"),
):
    q = Q()
    user_id = CTX_USER_ID.get()
    user = await user_controller.get(user_id)
    if user.space_id:
        q &= Q(space_id=user.space_id)
    if customer_id:
        q &= Q(user_id=customer_id)
    total, data = await cases_controller.do_list(page=page, page_size=page_size, search=q)

    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@saas_cases_router.get("/get", summary=f"查看{model_name}")
async def do_settings_get(
        data_id: int = Query(..., description="ID"),
):
    data = await cases_controller.do_detail(data_id=data_id)
    return Success(data=data)

@saas_cases_router.post("/create", summary=f"创建{model_name}")
async def do_settings_create(
        data_in: CasesCreate,
):
    await cases_controller.do_create(obj_in=data_in)
    return Success(msg="创建成功")

@saas_cases_router.post("/update", summary=f"更新{model_name}")
async def do_settings_update(
        data_in: CasesUpdate,
):
    await cases_controller.do_update(obj_in=data_in)
    return Success(msg="更新成功")

@saas_cases_router.delete("/delete", summary=f"删除{model_name}")
async def do_settings_delete(
        data_id: int = Query(..., description="ID"),
):
    await cases_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")
